let search = document.querySelector("#search");
search.onclick = function () {
    let title = document.querySelector("#searchInput");
    $.ajax({
        type: 'get',
        url: 'list' + "?searchTitle=" + encodeURIComponent(title.value),
        success: function (body) {
            // 获取的每个对象就是一个js对象
            let right = document.querySelector(".right");
            if (!body || body == ''){
                right.innerHTML = '<h2 style="color: whitesmoke;font-size: 16px; font-weight: lighter; margin: auto auto">未查询到相关内容。</h2>';
            }else {
                right.innerHTML = '';// 清空原有的
                for(let blog of body){
                    let blogDiv = document.createElement('div');
                    blogDiv.className = 'blog';
                    // 构造标题
                    let titleDiv = document.createElement('div');
                    titleDiv.innerHTML = blog.title;
                    titleDiv.className = 'title';
                    blogDiv.appendChild(titleDiv);

                    // 构造发布时间
                    let dateDiv = document.createElement('div');
                    dateDiv.innerHTML = blog.postTime;
                    dateDiv.className = 'date';
                    blogDiv.appendChild(dateDiv);
                    // 构造摘要
                    let descDiv = document.createElement('div');
                    descDiv.innerHTML = blog.content;
                    descDiv.className = 'desc';
                    blogDiv.appendChild(descDiv);

                    // 构造查看全文
                    let a = document.createElement('a');
                    a.innerHTML = '查看全文 &gt;&gt;';
                    // 跳转时,需要知道是哪篇博客
                    a.href = 'blogDetail.html?blogId=' + blog.blogId;
                    blogDiv.appendChild(a);

                    // 挂到right上
                    right.appendChild(blogDiv);
                }
            }
        },
        error: function () {
            alert("获取页面失败!");
        }
    });
}
